﻿namespace AnyOffice.general.knowledge
{
    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Web;

    public class KnowledgeFactory
    {
        public static void AddKnowledgeInfo(KnowledgeInfo diskInfo)
        {
            string path = HttpContext.Current.Server.MapPath("/attachment/helper/knowledge") + @"\" + HttpContext.Current.User.Identity.Name + ".bin";
            IFormatter formatter = new BinaryFormatter();
            Stream serializationStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(serializationStream, diskInfo);
            serializationStream.Close();
        }

        public static KnowledgeInfo GetKnowledgeInfo()
        {
            string path = HttpContext.Current.Server.MapPath("/attachment/helper/knowledge") + @"\" + HttpContext.Current.User.Identity.Name + ".bin";
            if (File.Exists(path))
            {
                IFormatter formatter = new BinaryFormatter();
                Stream serializationStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                KnowledgeInfo info = (KnowledgeInfo) formatter.Deserialize(serializationStream);
                serializationStream.Close();
                return info;
            }
            return null;
        }
    }
}

